
package com.fas.config;


import org.junit.Assert;

import redis.clients.jedis.Protocol;

import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.render.ViewType;

/**
 * 系统资源文件配置管理类
 * 
 * 		读取资源appConfig.properties文件
 * 
 * @Copyright: Copyright (c) 2016 FAS 
 * @anthor yixiaoping<thinker1013@163.com>
 * @since 2016年1月23日 下午2:45:20
 * @version V1.0
 */
public class AppConfig {
	private static AppConfig me = new AppConfig();
	private AppConfig() {
		init();
	}

	public static AppConfig me() {
		if(me == null)
			me = new AppConfig();
		return me;
	}
	private final static String APP_CONFIG_FILE = "appConfig.properties";
	
	private final static boolean DEFAULT_JF_DEVMODE = false;
	private final static String DEFAULT_JF_ENCODING = "UTF-8";
	private final static String DEFAULT_JF_500VIEW = "/page/public/error/500.jsp";
	private final static String DEFAULT_JF_404VIEW = "/page/public/error/404.jsp";
	private final static String DEFAULT_JF_403VIEW = "/page/public/error/403.jsp";
	private final static String DEFAULT_JF_401VIEW = "/page/public/error/401.jsp";
	private final static String DEFAULT_JF_BASEVIEWPATH = "/page";
	private final static String DEFAULT_JF_SEPARATOR = "&";
	private final static String DEFAULT_JF_UPLOADEDPATH = "uploadfile";
	
	private final static int  DEFAULT_JDBC_MAXPOOLSIZE = 100; 
	private final static int  DEFAULT_JDBC_MINPOOLSIZE = 5; 
	private final static int  DEFAULT_JDBC_INITIALPOOLSIZE = 5; 
	private final static int  DEFAULT_JDBC_MAXIDLETIME = 60; 
	private final static int  DEFAULT_JDBC_ACQUREINCREMENT = 5; 
	private final static boolean  DEFAULT_JDBC_SHOWSQL = false; 

	
	//jfinal
	private boolean JFDevMode;
	private ViewType JFViewType;
	private String JF401View;
	private String JF403View;
	private String JF404View;
	private String JF500View;
	private String JFBaseViewPath;
	private String JFSeparator;
	private String JFEncoding;
	private String JFUploadedFileSaveDirectory;
	
	//db
	private String jdbcUrl;
	private String jdbcUsername;
	private String jdbcPassword;
	private String jdbcSchema;
	private String jdbcDriverClassName;
	private String jdbcDialect;
	private int jdbcMaxPoolSize;
	private int jdbcMinPoolSize;
	private int jdbcInitialPoolSize;
	private int jdbcMaxIdleTime;
	private int jdbcAcquireIncrement;
	private boolean jdbcShowsql;
	
	//redis
	private String redisCacheName;
	private String redisHost;
	private int redisPort;
	private int redisTimeout;
	private String redisPassword;
	
	
	private void init(){
		Prop prop = PropKit.use(APP_CONFIG_FILE);
		Assert.assertNotNull("配置文件appConfig.properties加载失败", prop);
		
		initJFConfig(prop);
		initPoolConfig(prop);
		initRedisConfig(prop);

	}
	
	private void initJFConfig(Prop prop){
		this.setJFDevMode(prop.getBoolean("jfinal.DevMode", DEFAULT_JF_DEVMODE));
		this.setJFViewType(ViewType.JSP);
		this.setJFEncoding(DEFAULT_JF_ENCODING);
		this.setJF401View(DEFAULT_JF_401VIEW);
		this.setJF403View(DEFAULT_JF_403VIEW);
		this.setJF404View(DEFAULT_JF_404VIEW);
		this.setJF500View(DEFAULT_JF_500VIEW);
		this.setJFSeparator(DEFAULT_JF_SEPARATOR);
		this.setJFBaseViewPath(DEFAULT_JF_BASEVIEWPATH);
		this.setJFUploadedFileSaveDirectory(DEFAULT_JF_UPLOADEDPATH);
	}
	
	private void initPoolConfig(Prop prop){
		this.setJdbcUrl(prop.get("jdbc.url"));
		this.setJdbcUsername(prop.get("jdbc.username"));
		this.setJdbcPassword(prop.get("jdbc.password"));
		this.setJdbcDialect(prop.get("jdbc.dialect"));
		this.setJdbcSchema(prop.get("jdbc.schema",prop.get("jdbc.username")));
		this.setJdbcDriverClassName(prop.get("jdbc.driverClassName"));
		this.setJdbcShowsql(prop.getBoolean("jdbc.showsql",DEFAULT_JDBC_SHOWSQL));
		this.setJdbcMaxPoolSize(prop.getInt("jdbc.maxPoolSize", DEFAULT_JDBC_MAXPOOLSIZE));
		this.setJdbcMinPoolSize(prop.getInt("jdbc.minPoolSize", DEFAULT_JDBC_MINPOOLSIZE));
		this.setJdbcMaxIdleTime(prop.getInt("jdbc.maxIdleTime", DEFAULT_JDBC_MAXIDLETIME));
		this.setJdbcInitialPoolSize(prop.getInt("jdbc.initialPoolSize", DEFAULT_JDBC_INITIALPOOLSIZE));
		this.setJdbcAcquireIncrement(prop.getInt("jdbc.acquireIncrement", DEFAULT_JDBC_ACQUREINCREMENT));
	}

	private void initRedisConfig(Prop prop){
		this.setRedisCacheName(prop.get("redis.cacheName","redis"));
		this.setRedisHost(prop.get("redis.host", Protocol.DEFAULT_HOST));
		this.setRedisPort(prop.getInt("redis.port", Protocol.DEFAULT_PORT));
		this.setRedisPassword(prop.get("redis.password"));
		this.setRedisTimeout(prop.getInt("redis.timeout", Protocol.DEFAULT_TIMEOUT));
	}
	
	public boolean isJFDevMode() {
		return JFDevMode;
	}

	public void setJFDevMode(boolean jFDevMode) {
		JFDevMode = jFDevMode;
	}

	public ViewType getJFViewType() {
		return JFViewType;
	}

	public void setJFViewType(ViewType jFViewType) {
		JFViewType = jFViewType;
	}

	public String getJF500View() {
		return JF500View;
	}

	public void setJF500View(String jF500View) {
		JF500View = jF500View;
	}

	public String getJF404View() {
		return JF404View;
	}

	public void setJF404View(String jF404View) {
		JF404View = jF404View;
	}

	public String getJFBaseViewPath() {
		return JFBaseViewPath;
	}

	public void setJFBaseViewPath(String jFBaseViewPath) {
		JFBaseViewPath = jFBaseViewPath;
	}

	public String getJFSeparator() {
		return JFSeparator;
	}

	public void setJFSeparator(String jFSeparator) {
		JFSeparator = jFSeparator;
	}

	public String getJFUploadedFileSaveDirectory() {
		return JFUploadedFileSaveDirectory;
	}

	public void setJFUploadedFileSaveDirectory(String jFUploadedFileSaveDirectory) {
		JFUploadedFileSaveDirectory = jFUploadedFileSaveDirectory;
	}

	
	public String getJF401View() {
		return JF401View;
	}

	public void setJF401View(String jF401View) {
		JF401View = jF401View;
	}

	public String getJF403View() {
		return JF403View;
	}

	public void setJF403View(String jF403View) {
		JF403View = jF403View;
	}

	public String getJFEncoding() {
		return JFEncoding;
	}

	public void setJFEncoding(String jFEncoding) {
		JFEncoding = jFEncoding;
	}

	public String getJdbcUrl() {
		return jdbcUrl;
	}

	public void setJdbcUrl(String jdbcUrl) {
		this.jdbcUrl = jdbcUrl;
	}

	public String getJdbcUsername() {
		return jdbcUsername;
	}

	public void setJdbcUsername(String jdbcUsername) {
		this.jdbcUsername = jdbcUsername;
	}

	public String getJdbcPassword() {
		return jdbcPassword;
	}

	public void setJdbcPassword(String jdbcPassword) {
		this.jdbcPassword = jdbcPassword;
	}

	public String getJdbcDriverClassName() {
		return jdbcDriverClassName;
	}

	public void setJdbcDriverClassName(String jdbcDriverClassName) {
		this.jdbcDriverClassName = jdbcDriverClassName;
	}

	public int getJdbcMaxPoolSize() {
		return jdbcMaxPoolSize;
	}

	public void setJdbcMaxPoolSize(int jdbcMaxPoolSize) {
		this.jdbcMaxPoolSize = jdbcMaxPoolSize;
	}

	public int getJdbcMinPoolSize() {
		return jdbcMinPoolSize;
	}

	public void setJdbcMinPoolSize(int jdbcMinPoolSize) {
		this.jdbcMinPoolSize = jdbcMinPoolSize;
	}

	public int getJdbcInitialPoolSize() {
		return jdbcInitialPoolSize;
	}

	public void setJdbcInitialPoolSize(int jdbcInitialPoolSize) {
		this.jdbcInitialPoolSize = jdbcInitialPoolSize;
	}

	public int getJdbcMaxIdleTime() {
		return jdbcMaxIdleTime;
	}

	public void setJdbcMaxIdleTime(int jdbcMaxIdleTime) {
		this.jdbcMaxIdleTime = jdbcMaxIdleTime;
	}

	public int getJdbcAcquireIncrement() {
		return jdbcAcquireIncrement;
	}

	public void setJdbcAcquireIncrement(int jdbcAcquireIncrement) {
		this.jdbcAcquireIncrement = jdbcAcquireIncrement;
	}

	public String getJdbcDialect() {
		return jdbcDialect;
	}

	public void setJdbcDialect(String jdbcDialect) {
		this.jdbcDialect = jdbcDialect;
	}

	public boolean isJdbcShowsql() {
		return jdbcShowsql;
	}

	public void setJdbcShowsql(boolean jdbcShowsql) {
		this.jdbcShowsql = jdbcShowsql;
	}

	public String getJdbcSchema() {
		return jdbcSchema;
	}

	public void setJdbcSchema(String jdbcSchema) {
		this.jdbcSchema = jdbcSchema;
	}

	public String getRedisCacheName() {
		return redisCacheName;
	}

	public void setRedisCacheName(String redisCacheName) {
		this.redisCacheName = redisCacheName;
	}

	public String getRedisHost() {
		return redisHost;
	}

	public void setRedisHost(String redisHost) {
		this.redisHost = redisHost;
	}

	public int getRedisPort() {
		return redisPort;
	}

	public void setRedisPort(int redisPort) {
		this.redisPort = redisPort;
	}

	public int getRedisTimeout() {
		return redisTimeout;
	}

	public void setRedisTimeout(int redisTimeout) {
		this.redisTimeout = redisTimeout;
	}

	public String getRedisPassword() {
		return redisPassword;
	}

	public void setRedisPassword(String redisPassword) {
		this.redisPassword = redisPassword;
	}
}
